//
//  ActivityViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-19.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityCell.h"
#import "Activity.h"
#import "Contact.h"
#import "DateMethod.h"
#import "ActivityDetailViewController.h"
#import "PublishActivityViewController.h"

@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate>{
    IBOutlet UITableView  *activityTableView;
    NSArray *activityArray;
}

@end

@implementation ActivityViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
      self.title = @"活动";
    activityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self initData];
    //设置UIBarButtonItem 背景图片
    UIImage *sureImage = [UIImage imageNamed:@"publish_icon.png"];
    //初始化一个UIButton
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置UIButton.bounds的位置大小
    sureButton.bounds = CGRectMake( 0, 0, sureImage.size.width, sureImage.size.height );
    //设置UIButton的背景图片
    [sureButton setImage:sureImage forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(publishActivity:) forControlEvents:UIControlEventTouchUpInside];
    //通过initWithCustomView：方法把UIButton加导UIBarButtonItem中
    UIBarButtonItem *sureButtonItem = [[UIBarButtonItem alloc] initWithCustomView:sureButton];
    
    self.navigationItem.rightBarButtonItem = sureButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

-(void)initData{
    Activity *activity =[[Activity alloc]init];
    activity.activityID = @"123";
    activity.initiatorID = @"123456";
    activity.time = [NSDate date];
    activity.subject = @"今晚有没有人一块儿去看电影";
    activity.beginDate = [NSDate date];
    activity.endDate = [NSDate date];
    activity.place = @"永旺国际商城";
    activity.content = @"大家每人带100块钱和一杯可乐!今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,大家每人带100块钱和一杯可乐!今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影,今晚有没有人一块儿去看电影";
    activity.joinArray = nil;
    activity.commentArray = nil;
    
    Contact *contact = [[Contact alloc]init];
    contact.userID = @"123456";
    contact.name = @"李雷川";
    contact.phone = @"3358398";
    contact.mobilephone = @"18010027533";
    contact.officeNo= @"306";
    activity.initiator = contact;
    
    activityArray = @[activity,activity,activity,activity];
    
}

-(void)publishActivity:(id)sender{
    PublishActivityViewController *publishController = [self.storyboard instantiateViewControllerWithIdentifier:@"PublishActivityViewController"];
    [self presentViewController:publishController animated:YES completion:^{
        
    }];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    Activity *activity = activityArray[indexPath.row];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,200,20)];
    label.numberOfLines = 0;
    label.font =[UIFont systemFontOfSize:16];
    label.text = activity.subject;
    CGSize subjectSize =   [label sizeThatFits:CGSizeMake(200, 500)];
    
    label.text = activity.content;
    label.font =[UIFont systemFontOfSize:14];
    CGSize detailSize =   [label sizeThatFits:CGSizeMake(200, 500)];
  
    float defalutHeight =subjectSize.height + detailSize.height + 80;
    return defalutHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return activityArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityCell *  cell = (ActivityCell *)[tableView dequeueReusableCellWithIdentifier:@"Activity"];
    if (!cell) {
        cell = [[ActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Activity"];
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
  
    Activity *activity = activityArray[indexPath.row];
    [cell initWithActivity:activity];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *path = [activityTableView indexPathForSelectedRow];
    Activity *activity = activityArray[path.row];
    ActivityDetailViewController *activityDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ActivityDetailViewController"];
    [activityDetailViewController initWithActivity:activity];
    [self.navigationController pushViewController:activityDetailViewController animated:YES];
}

@end
